<?php
declare (strict_types=1);
namespace app\middleware;

use app\utils\JwtUtil;

class CheckSign
{
    /**
     * @param $request
     * @param \Closure $next
     * @return mixed|\think\response\Json
     * 处理请求
     */
    public function handle($request, \Closure $next)
    {
        if(!in_array($request->controller(),['Login'])){  //对控制器放行
            $token = $request->header('X-token'); //前端携带的token信息
            $jwt = JwtUtil::verification('test',$token); //是否与签发的key一致
            if($jwt->getData()['code'] == 20000){
                $request->uid = $jwt->getData()['data']->data->uid; //向控制器传递当前用户id
                $request->role_key = $jwt->getData()['data']->data->role; //向控制器传递当前用户角色
            }else{
                return $jwt;
            }
        }
        return $next($request);
    }
}